﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;

namespace SimpleSM
{
    public partial class Options : Form
    {
        public Options()
        {
            InitializeComponent();
        }

        private void btAdd_Click(object sender, EventArgs e)
        {
            new TypeSession().ShowDialog();
            this.RefreshDGV();
        }

        private void RefreshDGV()
        {
            dataGridView1.Rows.Clear();
            DataTable dt = new TSession().Get();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dataGridView1.Rows.Add(dt.Rows[i][0].ToString(), (i + 1).ToString(), dt.Rows[i][1].ToString(), dt.Rows[i][2].ToString(), dt.Rows[i][3].ToString(), dt.Rows[i][4].ToString());
            }
        }

        private void Options_Load(object sender, EventArgs e)
        {
            tbDB.Text = Properties.Settings.Default.database;
            tbBackupDir.Text = Properties.Settings.Default.backupDir;
            cbDBBackup.SelectedIndex = Properties.Settings.Default.backupDB;
            tbPagentPath.Text = Properties.Settings.Default.pagentPath;
            if (Properties.Settings.Default.pagentFiles != "")
            {
                foreach (String p in Properties.Settings.Default.pagentFiles.Split(';'))
                {
                    listBox1.Items.Add(p);
                }
            }
            chbAutoStart.Checked = Properties.Settings.Default.autostartPagent;
            this.RefreshDGV();
        }

        private void btPagentSelect_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "EXE|*.exe|All files|*.*";
            openFileDialog1.FilterIndex = 0;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                tbPagentPath.Text = openFileDialog1.FileName;
                Properties.Settings.Default.pagentPath = openFileDialog1.FileName;
                Properties.Settings.Default.Save();
            }
        }

        private void btAddKey_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "Putty key|*.ppk|All files|*.*";
            openFileDialog1.FilterIndex = 0;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                listBox1.Items.Add(openFileDialog1.FileName);
                string s = "";
                for (int i = 0; i < listBox1.Items.Count; i++)
                {
                    s += listBox1.Items[i].ToString() + ";";
                }
                s = s.Substring(0, s.Length - 1);
                Properties.Settings.Default.pagentFiles = s;
                Properties.Settings.Default.Save();
            }
        }

        private void btRemoveKey_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedItem != null)
            {
                listBox1.Items.Remove(listBox1.SelectedItem);
                string s = "";
                for (int i = 0; i < listBox1.Items.Count; i++)
                {
                    s += listBox1.Items[i].ToString() + ";";
                }
                if (s != "")
                {
                    s = s.Substring(0, s.Length - 1);
                }
                Properties.Settings.Default.pagentFiles = s;
                Properties.Settings.Default.Save();
            }
        }

        private void btRestartPagent_Click(object sender, EventArgs e)
        {
            if (Properties.Settings.Default.pagentPath != "")
            {
                FileInfo fi = new FileInfo(Properties.Settings.Default.pagentPath);
                foreach (var process in Process.GetProcessesByName(fi.FullName))
                {
                    process.Kill();
                }
                ProcessStartInfo startInfo = new ProcessStartInfo();
                startInfo.FileName = Properties.Settings.Default.pagentPath;
                StringBuilder args = new StringBuilder();
                foreach (string s in Properties.Settings.Default.pagentFiles.Split(';'))
                {
                    args.Append(" \"" + s + "\"");
                }
                startInfo.Arguments = @args.ToString();
                Process.Start(startInfo);
            }
        }

        private void btEdit_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedCells.Count > 0)
            {
                new TypeSession(int.Parse(dataGridView1.Rows[dataGridView1.SelectedCells[0].RowIndex].Cells[0].Value.ToString())).ShowDialog();
                this.RefreshDGV();
            }
        }

        private void btDel_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                if (MessageBox.Show("Do you really delete this type?", "Warrning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                {
                    new TSession().Delete(int.Parse(dataGridView1.Rows[dataGridView1.SelectedCells[0].RowIndex].Cells[0].Value.ToString()));
                    this.RefreshDGV();
                }
            }
        }

        private void btDBBackupDirChange_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                tbBackupDir.Text = folderBrowserDialog1.SelectedPath;
                Properties.Settings.Default.backupDir = folderBrowserDialog1.SelectedPath;
                Properties.Settings.Default.Save();
            }
        }

        private void cbDBBackup_SelectedIndexChanged(object sender, EventArgs e)
        {
            Properties.Settings.Default.backupDB = cbDBBackup.SelectedIndex;
            Properties.Settings.Default.Save();
        }

        private void chbAutoStart_CheckedChanged(object sender, EventArgs e)
        {
            Properties.Settings.Default.autostartPagent = chbAutoStart.Checked;
            Properties.Settings.Default.Save();
        }
    }
}
